<?php
/*
Feature Name:	Comment AJAX
Feature URI:	http://www.inn-studio.com
Version:		1.0.1
Description:	Use AJAX when browse/add/reply comment. (Recomend to enable)
Author:			INN STUDIO
Author URI:		http://www.inn-studio.com
*/
add_action('seajs_use','comments_ajax::js');
add_filter('theme_process','comments_ajax::process');
class comments_ajax{
	static function tdomain(){
		load_theme_textdomain('LANG', dirname(__FILE__). '/languages');
	}
	static function process($output){
		self::tdomain();
		/**
		 * Check the ajax comment post
		 */
		if(isset($_POST['comment_post_ID'])){
			global $wp_query,$comment, $comments, $post, $wpdb, $authordata,$current_user;
			$comment_post_ID = (int)$_POST['comment_post_ID'];
			$post = get_post($comment_post_ID);
			/**
			 * Get the post status
			 */
			$status = get_post_status($post);
			$status_obj = get_post_status_object($status);
			$authordata = get_userdata($post->post_author);
			/**
			 * Define original status
			 */
			$output['status'] = 'success';
			/**
			 * If no found the comment
			 */
			if(empty($post->comment_status)){
				do_action('comment_id_not_found', $comment_post_ID);
				$output['status'] = 'error';
				$output['des']['content'] = __('Invalid comment status.','LANG');
			/**
			 * If the comment is closed
			 */
			} elseif(!comments_open($comment_post_ID)){
				do_action('comment_closed', $comment_post_ID);
				$output['status'] = 'error';
				$output['des']['content'] = __('Sorry, comments are closed for this item.','LANG');
			/**
			 * If the post is trash
			 */
			} elseif('trash' == $status){
				do_action('comment_on_trash', $comment_post_ID);
				$output['status'] = 'error';
				$output['des']['content'] = __('Invalid comment status.','LANG'); 
			/**
			 * If the post is draft
			 */
			} elseif(!$status_obj->public && !$status_obj->private){
				do_action('comment_on_draft', $comment_post_ID);
				$output['status'] = 'error';
				$output['des']['content'] = __('Invalid comment status.','LANG'); 
			/**
			 * If the post needs password
			 */
			} elseif(post_password_required($comment_post_ID)){
				do_action('comment_on_password_protected', $comment_post_ID);
				$output['status'] = 'error';
				$output['des']['content'] = __('Password Protected','LANG');
			/**
			 * Pass all
			 */
			} else{
				do_action('pre_comment_on_post', $comment_post_ID);
				/**
				 * Define comment values
				 */
				$comment_author			= isset($_POST['author'])			? trim(strip_tags($_POST['author'])): null;
				$comment_author_email	= isset($_POST['email'])			? trim($_POST['email']): null;
				$comment_author_url		= isset($_POST['url'])				? trim($_POST['url']): null;
				$comment_content		= isset($_POST['comment'])			? trim(htmlentities($_POST['comment'],null,'UTF-8')): null;
				$comment_parent			= isset($_POST['comment_parent'])	? (int)trim($_POST['comment_parent']): null;
				$comment_agant 			= $_SERVER['HTTP_USER_AGENT'];
				$comment_author_ip 		= get_client_ip();
				$comment_date 			= current_time('mysql');

				/**
				 * Get the user is login or not
				 */
				$user = wp_get_current_user();		
				/**
				 * If logged
				 */
				if($user->ID){
					if(empty($user->display_name)){
						$user->display_name	= $user->user_login;
					}
					$comment_author       = $wpdb->escape($user->display_name);
					$comment_author_email = $wpdb->escape($user->user_email);
					$comment_author_url   = $wpdb->escape($user->user_url);
					if(current_user_can('unfiltered_html')){
						if(wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID)!= $_POST['_wp_unfiltered_html_comment']){
							kses_remove_filters(); // start with a clean slate
							kses_init_filters(); // set up the filters
						}
					}
				/**
				 * If not login, just visitor
				 */
				}else{
					if(get_option('comment_registration')|| 'private' == $status){
						$output['status'] = 'error';
						$output['des']['content'] = __('Sorry, you must be logged in to post a comment.','LANG');
					}
				}
				/**
				 * Check required 
				 */
				if(get_option('require_name_email')&& !$user->ID){
					if(6 > strlen($comment_author_email)|| '' == $comment_author){
						$output['status'] = 'error';
						$output['des']['content'] = __('Error: please fill the required fields(name, email).','LANG');
					}else if(!is_email($comment_author_email)){
						$output['status'] = 'error';
						$output['des']['content'] = __('Error: please enter a valid email address.','LANG');
					}
				}
				/**
				 * If no comment content
				 */
				if(!$comment_content){
					$output['status'] = 'error';
					$output['des']['content'] = __('Error: please type a comment.','LANG');
				}
				/**
				 * Compact the information
				 */
				$commentdata = compact(
					'comment_post_ID',
					'comment_author', 
					'comment_author_email', 
					'comment_author_url', 
					'comment_content',
					'comment_type',
					'comment_parent',
					'user_ID'
				);
				/**
				 * Insert new comment and get the comment ID
				 */
				$comment_id = wp_insert_comment($commentdata);
				/**
				 * Update new comment
				 */
				wp_update_comment_count($comment_post_ID);
				/**
				 * Get new comment and set cookie
				 */
				$comment = get_comment($comment_id);
				if(!$user->ID){
					$comment_cookie_lifetime = apply_filters('comment_cookie_lifetime', 30000000);
					setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, time()+ $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
					setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time()+ $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
					setcookie('comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time()+ $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
				}
				/**
				 * Class style
				 */
				$comment_depth = 1;
				$tmp_c = $comment;
				while($tmp_c->comment_parent != 0){
					$comment_depth++;
					$tmp_c = get_comment($tmp_c->comment_parent);
				}
				
				/**
				 * Check if error or not
				 */
				if($output['status'] !== 'error'){
					/**
					 * Check if Reply comment
					 */
					if($comment_parent != 0){
						$output['des']['comment_parent'] = $comment_parent;
						$output['des']['content'] = '<ul id="children_'.$comment->comment_ID.'" class="children children_'.$comment->comment_ID.'">'.theme_functions::get_comment_list().'</ul>';
					}else{
						$output['des']['content'] = theme_functions::get_comment_list();
					}
					$output['des']['post_id'] = $comment_post_ID;
				}
			}
		}
		return $output;
	}
	static function js(){
		if(is_singular()){
			self::tdomain();
			?>
			seajs.use('<?php echo theme_features::get_theme_features_dir(basename(dirname(__FILE__)). '/init.js');?>',function(comments_ajax){
				comments_ajax.M00001 = '<?php _e('Commented successfully, thanks your participation!','LANG');?>';
				comments_ajax.init();
			});
			<?php
		}
	}
}

?>